package swing;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableRowSorter;

import swing.LogTable.DateCellRenderer;

import lib.DictionaryTableModel;

public class DictionaryTable extends JPanel{
	private JTable table;
	private DictionaryTableModel tableModel;
	private static JTabbedPane tabPane=null;
	
	public DictionaryTable(JTabbedPane tabbedPane) {
		setBorder(new LineBorder(new Color(0, 0, 0)));
		this.tabPane=tabbedPane;
		setLayout(null);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		double width = screenSize.getWidth();
		double height = screenSize.getHeight();
		tableModel = new DictionaryTableModel();
		
		table = new JTable(tableModel);
		table.setPreferredScrollableViewportSize(new Dimension((int) (width-200),(int)height-200));
		table.setFillsViewportHeight(true);
		
		//TableColumnModel tcm = table.getColumnModel();
		//tcm.getColumn(0).setMaxWidth(40);
		//tcm.getColumn(1).setMaxWidth(120);
		//tcm.getColumn(0).setPreferredWidth(40);
		//tcm.getColumn(1).setPreferredWidth(120);
		//tcm.getColumn(1).setCellRenderer(new DateCellRenderer());
		JScrollPane scroll= new JScrollPane(table);
		scroll.setBounds(6, 5, (int) (width-140), (int)height-100);
		add(scroll);
		
		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//tabPane.setSelectedIndex(1);
				tableModel.saveToDatabase();
			}
		});
		btnSave.setBounds((int) (width-100), 41, 80, 41);
		add(btnSave);
		
		JButton btnRefresh = new JButton("Refresh");
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				tableModel.recoveryData();
			}
		});
		btnRefresh.setBounds(1266, 93, 80, 41);
		add(btnRefresh);
		
		DefaultTableCellRenderer leftRenderer = new DefaultTableCellRenderer();
		leftRenderer.setHorizontalAlignment( JLabel.LEFT );
		table.setDefaultRenderer(String.class, leftRenderer);
		table.setDefaultRenderer(Integer.class, leftRenderer);
		
		//TableRowSorter t1 = new TableRowSorter(tableModel);
		//t1.setComparator(DictionaryTableModel.DICTIONARYID_INDEX, comparator);
		//TableRowSorter t2 = new TableRowSorter(tableModel);
		//t2.setComparator(DictionaryTableModel.TOPICID_INDEX, comparator);
		//table.setRowSorter(t1);
		//table.setRowSorter(t2);
		table.setAutoCreateRowSorter(true);
	}
	
}
